package com.itheima.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.store.Course;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *  course的增删改查
 *  2021/10/16
 *  create by guowenliang
 */
@WebServlet(urlPatterns = "/store/course")
public class CourseServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        // 1.获取参数
        String operation = request.getParameter("operation");
        // 获取当前用户信息

        // a
        String username = (String) request.getSession().getAttribute("username");
        if ("list".equals(operation)){
            list(request,response);
        }else if ("toAdd".equals(operation)) {
            toAdd(request,response);
        }else if ("save".equals(operation)){
            save(request,response,username);
        }else if ("toEdit".equals(operation)){
            toEdit(request,response);
        }else if ("edit".equals(operation)){
            edit(request,response);
        }else if ("delete".equals(operation)){
            delete(request,response);
        }

        response.setContentType("text/html;charset=UTF-8");
    }

    // 删除
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数
        String id = request.getParameter("id");
        // 调用服务层
        courseService.deleteCourse(id);
        // 数据响应
        request.getRequestDispatcher(request.getContextPath()+"/store/course?operation=list").forward(request,response);
    }

    // 保存修改
    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        String state = request.getParameter("state");
        Course course = new Course();
        course.setId(id);
        course.setName(name);
        course.setRemark(remark);
        course.setState(state);
        // 调用服务层
        courseService.updateCourse(course);
        // 数据响应
        request.getRequestDispatcher(request.getContextPath()+"/store/course?operation=list").forward(request,response);
    }

    // 返回编辑界面
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数
        String id = request.getParameter("id");
        // 调用服务层
        Course course = courseService.findCourseById(id);
        // 数据响应
        request.setAttribute("course",course);
        request.getRequestDispatcher(request.getContextPath()+"/WEB-INF/pages/store/course/update.jsp").forward(request,response);
    }

    // 添加
    private void save(HttpServletRequest request, HttpServletResponse response, String username) throws ServletException, IOException {
        request.getRequestDispatcher("utf-8");
        response.setContentType("text/html;charset=utf-8");
        // 获取参数
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        String state = request.getParameter("state");
        Course course = new Course();
        course.setName(name);
        course.setRemark(remark);
        course.setState(state);
        // 调用服务层
        courseService.addCourse(course,username);
        // 数据响应
        request.getRequestDispatcher(request.getContextPath()+"/store/course?operation=list").forward(request,response);
        // list(request,response);
    }

    // 返回添加界面
    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 响应数据
        request.getRequestDispatcher(request.getContextPath()+"/WEB-INF/pages/store/course/add.jsp").forward(request,response);
    }

    // 查询全部，使用分页查询
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取参数
        String pageNum = request.getParameter("page");
        // 2.调用服务层
        PageInfo page = courseService.findByPage(pageNum);
        // 3.处理结果

        // 4.响应数据
        request.setAttribute("page",page);
        request.getRequestDispatcher(request.getContextPath()+"/WEB-INF/pages/store/course/list.jsp").forward(request,response);
    }

    // 查询全部
    private List<Course> findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 2.调用服务层
        List<Course> courseList = courseService.findAll();
        return courseList;
    }

}